<template>
  <div class="page-container">
    <!-- 轮播图 -->
    <div style="display: flex;justify-content: center;background: linear-gradient(360deg, #ffffff, #082a60);">
      <section class="hero">
        <swiper 
            style="border-radius: 8px;height:400px"
            :modules="[Autoplay, Navigation, Pagination]"
            :pagination="{
              el: '.swiper-pagination',
              clickable: true,
              type: 'bullets'
            }"
            :autoplay="{ delay: 2000 }" 
            loop 
            :navigation="{
              nextEl: '.swiper-button-next',
              prevEl: '.swiper-button-prev'
            }"
          >
          <swiper-slide v-for="item in carouselData" :key="item.semester_id">
            <div class="hero-slide" :style="{ backgroundImage: `url(${item.cover})` }">
              <div class="hero-overlay">
                <div class="overlay-content">
                  <h1>{{ item.semester_name }}</h1>
                  <p>课程ID: {{ item.semester_id }}</p>
                </div>
              </div>
            </div>
          </swiper-slide>
          <div class="swiper-navigation">
            <div class="swiper-button-prev"></div>
            <div class="swiper-button-next"></div>
          </div>
          <div class="swiper-pagination"></div>
        </swiper>
      </section>
    </div>
    <!-- 项目成就 -->
    <section class="achievements">
      <div>
        <h3 class="section-subtitle">平台数据</h3>
        <h2 class="section-title">-OUR REACHED-</h2>
      </div>
      <div class="stats scroll-animation">
        <div v-for="(item, index) in stats" :key="index" class="stat">
          <router-link to="/course-introduction"> 
          <h1>{{ item.value }}</h1>  
          </router-link>
          <p>{{ item.label }}</p>
          <span>{{ item.clabel }}</span>
        </div>
      </div>
    </section>



    <!-- 团队介绍 -->
    <section class="team">
      <h3 class="section-subtitle" style="color: #ffffff40;text-shadow: 5px -5px 0px #ffffff;">教学团队</h3>
      <h2 class="section-title" style="margin-bottom: 80px;color:#8a8a8a;">-OUR TEAMS-</h2>
      <div class="team-box" scroll-animation>
        <div v-for="(member, index) in team.slice(0,2)" :key="index" class="team-member">
          <div style="width:50%"  v-if="index % 2 === 0">
            <router-link to="/team">
              <img :src="member.image" alt="Team member" class="team-img" style="height: 300px;width: 28vw;object-fit: cover;">
            </router-link>
           </div>
          <div class="team-info">
            <div style="display: flex;flex-direction: row;align-items: flex-end;margin-bottom: 20px;">
              <span style="font-size:2em;font-weight:bold;margin-right:40px;font-weight: bold;color:#083171">| {{ member.name }}</span>
              <span style="font-size:1.5em;font-weight:bold;color:#E5E5E5;font-weight: bold;">{{ member.role }}</span>
            </div>
            <p style="alin">{{ member.description }}</p>
          </div>
          <div style="width:50%"  v-if="index % 2 !== 0">
            <router-link to="/team">
              <img :src="member.image" alt="Team member" class="team-img" style="height: 300px;width: 28vw;object-fit: cover;">
            </router-link>
           </div>
        </div>

      </div>
    </section>

    <!-- 方向 -->
    <section class="direction">
      <div style="margin-bottom: 80px;align-items: center;">
        <h3 class="section-subtitle">教学方向</h3>
        <h2 class="section-title">-DIRECTION-</h2>
      </div>
      <div class="direction-grid scroll-animation" style="display:flex;align-items:left;">
        <div v-for="(item, index) in directions" :key="index" class="direction-item">
          <router-link to="/course-introduction">
            <img :src="item.image" alt="Direction">
          </router-link>
          <div class="direction-overlay">
            <h3 class="yg-title">{{ item.title }}</h3>
            <p class="yg-desc">{{ item.description }}</p>
          </div>
        </div>
      </div>
    </section>

    <!-- 用户评价 -->
    <section class="reviews">
      
      <div>
        <div class="comments">
          <h3 class="section-subtitle">用户评价</h3>      
          <h2 class="section-title" style="margin-bottom: 80px;">-EVALUATE-</h2>
          <div class="comments_items scroll-animation">
            <router-link to="/qa">
            <div class="com_item" v-for="(list,index) in commentList" :key="index">
              <!-- 评论头像 -->
              <div class="com_icon">
                <img :src="list.src" width="80" height="80">
              </div>
              <div class="com_cnt">
                <div class="cnt_name">
                  <!-- 评论人 -->
                  <span>{{list.name}}</span>
                  <!-- 评论时间 -->
                  <span>{{list.time}}</span>
                </div>
                <!-- 评论内容 -->
                <p class="cnt_text">{{list.content}}</p>
              </div>
              <!-- 点赞数量 -->
              <div class="com_num">
                <img src="@/assets/picture/like.png">
              <span> {{list.number}}</span>
              </div>
            </div>  
            </router-link>
          </div>
      </div>
      </div>
    </section>

    <!-- 教学功能 -->
    <section class="features">
      <h3 class="section-subtitle">教学功能</h3>
      <h2 class="section-title" style="margin-bottom: 80px;">-FUNCTIONS-</h2>
       <router-link to="/course-introduction"> 
      <div class="tools-container scroll-animation">
        <div class="tool-item" v-for="(tool, index) in tools" :key="index">
          <div class="tool-icon">
            <img :src="tool.icon" alt="工具图标">
          </div>
          <div class="tool-content">
            <h4>{{ tool.title }}</h4>
            <p>{{ tool.description }}</p>
          </div>
        </div>
      </div>
      </router-link>
    </section>
  </div>
</template>

<script>
import { getPicture } from '@/services/home';
import { Swiper, SwiperSlide } from 'swiper/vue';
import 'swiper/css';
import { Autoplay, Navigation, Pagination } from 'swiper/modules';
import { onMounted,ref } from 'vue';
import { 
  getHomeDirections,
  getCourseTeam,
  getPlatformData,
  getFunctions 
} from '@/services/api';

export default {
  components: { Swiper, SwiperSlide },
  setup() {
    const carouselData = ref([]);
    const directions = ref([]);
    const team = ref([]);
    const stats = ref([]);
    const tools = ref([]);


       onMounted(() => {
           getPicture().then(res => {
            if(res.data.status === 1) {
              carouselData.value = res.data.data;
            }
          });

          Promise.all([
      getHomeDirections(),
      getCourseTeam(1),
      getPlatformData(),
      getFunctions()
    ]).then(([dirRes, teamRes, statsRes, funcRes]) => {
      if(dirRes.data.status === 1) {
        directions.value = dirRes.data.data.map(item => ({
          title: item.direction_title,
          description: item.description,
          image: item.image
        }));
      }
      
      if(teamRes.data.status === 1) {
        team.value = teamRes.data.data.map(member => ({
          name: member.username,
          role: member.english_name,
          image: member.tech_pic,
          description: member.intro
        }));
      }
      
      if(statsRes.data.status === 1) {
        stats.value = [
          { clabel: '累计注册用户数', label: 'USERS', value: statsRes.data.data.user_cnt },
          { clabel: '累计课堂资源数', label: 'RESOURCE', value: statsRes.data.data.resource_cnt },
          { clabel: '累计课堂浏览数', label: 'VISITS', value: statsRes.data.data.visit_cnt }
        ];
      }
      
      if(funcRes.data.status === 1) {
        tools.value = funcRes.data.data.map(tool => ({
          ...tool,
          icon: require('@/assets/picture/' + (tool.cover+'.png' || 'default-icon.png')) // 根据实际图标路径调整
        }));
      }
    });


          

      const observer = new IntersectionObserver((entries) => {
        entries.forEach(entry => {
          // 添加 intersectionRatio 条件判断
          if (entry.isIntersecting || entry.intersectionRatio > 0) {
            entry.target.classList.add('scroll-visible');
          } else {
            entry.target.classList.remove('scroll-visible'); // 离开视口时重置状态
          }
        });
      }, { 
        threshold: Array.from({ length: 100 }, (_, i) => i * 0.01), // 更精细的阈值检测
        rootMargin: '0px 0px -100px 0px' // 底部 100px 提前触发
      });

      document.querySelectorAll('.scroll-animation').forEach((el) => {
        observer.observe(el);
        // 初始化时强制触发一次
        el.classList.toggle('scroll-visible', el.getBoundingClientRect().top < window.innerHeight);
      });
    });

    return {
       directions,
    team,
    stats,
    tools,
      Autoplay,
        carouselData,
        Navigation,
        Pagination,
         commentList:[
      {src:require("@/assets/picture/学生4.png"),name:"黄同学",time:"2024/12/02 13:10",content:"干货满满，超实用！",number:18},
      {src:require("@/assets/picture/学生2.png"),name:"李同学",time:"2024/12/22 06:40",content:"一个非常好的网站！",number:18},
      {src:require("@/assets/picture/老师1.png"),name:"王老师",time:"2025/02/06 16:48",content:"教学变得非常方便。",number:18},
      {src:require("@/assets/picture/学生3.png"),name:"陈同学",time:"2025/04/12 11:32",content:"学到了很多，推荐！",number:18}
    ],
    };
  },
};
</script>


<style scoped>
@import '@/assets/css/swiper.min.css';
@import '@/assets/css/font.css';
@import "@/assets/css/homepage.css";
/* 全局字体与颜色 */
body {
  font-family: 'Poppins', sans-serif;
  margin: 0;
  padding: 0;
  background: #f8f9fa;
}

.swiper-pagination {
  position: absolute;
  bottom: 20px !important;
  display: flex;
  justify-content: center;
  gap: 8px;
  width: 100%;
}
.swiper-navigation {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 97%;
  z-index: 2;
  display: flex;
  justify-content: space-between;
  padding: 0 20px;
}

.swiper-button-prev,
.swiper-button-next {
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s ease;
  background-color: rgba(255, 255, 255, 0.412);
  border-radius: 50%;
  width: 50px;
  height: 50px;
  display: flex;
  align-items: center;
}
.hero:hover .swiper-button-prev,
.hero:hover .swiper-button-next {
  opacity: 1;
  pointer-events: auto;
}
.swiper-button-prev:hover,
.swiper-button-next:hover {
  background-color: rgba(255, 255, 255, 0.9); /* 浅白色背景 */
  transform: scale(1.1);
}

.swiper-button-prev:hover::after,
.swiper-button-next:hover::after {
  color: #082a60; /* 悬停时蓝色图标 */
  transition: color 0.3s ease; /* 添加颜色过渡效果 */
}
/* 统一标题样式 */
.section-title {
font-size: 2rem;
    font-weight: bold;
    color: rgba(0, 0, 0, 0.162);
    text-align: center;
    position: relative;
    /* margin-bottom: -60px; */
    font-weight: 100;
    /* text-shadow: 6px -5px 1px black; */
    margin-top: -40px;
}
.swiper-pagination-bullet {
  background: #fff;
  opacity: 0.7;
  transition: all 0.3s ease;
}

.swiper-pagination-bullet-active {
  width: 12px;
  height: 12px;
  background: #082a60;
  border-radius: 50%;
}
.section-subtitle {
font-size: 2.8rem;
    font-weight: bold;
    color: #64646440;
    text-align: center;
    margin-bottom: 30px;
    text-shadow: 5px -5px 0px #3f3f3f;
}

/* 轮播图 */
.hero {
  width: 90%;
  max-width: 1200px;
  height: 400px;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.2);
  margin-top: 20px;
}

.hero-slide {
  width: 100%;
  height: 100%;
  background-size: cover;
  background-position: center;
  position: relative;
  display: flex;
  align-items: flex-end;
  justify-content: flex-start;
}

.hero-overlay {
  width: 100%;
  height: 100%;
  background: linear-gradient(to top, rgba(8, 42, 96, 0.85), rgba(0, 0, 0, 0.2));
  display: flex;
  align-items: flex-end;
  padding: 30px;
  box-sizing: border-box;
  color: #fff;
}

.overlay-content {
  max-width: 500px;
}

.overlay-content h1 {
  font-size: 2.4rem;
  font-weight: bold;
  margin-bottom: 10px;
  text-shadow: 2px 2px 8px rgba(0,0,0,0.3);
}

.overlay-content p {
  font-size: 1.1rem;
  text-shadow: 1px 1px 4px rgba(0,0,0,0.2);
}

.hero-title {
  font-size: 3rem;
  font-weight: bold;
}

.hero-subtitle {
  font-size: 1.5rem;
}

/* 项目成就 */
.achievements {
  text-align: center;
  padding: 50px 20px 150px 20px;
  background: white;
  
}

.stats {
  display: flex;
  justify-content: center;
  gap: 150px;
  margin-top: -40px;
  margin-bottom: 10px;
}

.stat h1 {
  font-size: 5rem;
  color: #194ea3;
  margin-bottom: 5px;
  font-weight: 500;
}

.stat p {
  font-size: 1.2rem;
  color: rgb(117, 117, 117);
  margin-top: 0px;
  margin-bottom: 50px;
  font-weight: bold;
}

.stat span {
  font-size: 1.rem;
  color: rgb(134, 134, 134);
}

/* 团队介绍 */
.team {
  text-align: center;
  background: #f8f9fa;
  padding: 50px 20px 100px 20px;
      display: flex;
    flex-direction: column;
    align-items: center;
    background-image: url(@/assets/picture/homebac1.jpg);
}

.team-box {
  background: white;
  flex-wrap: wrap;
  justify-content: center;
  gap: 60px;
  display: flex;
  flex-direction: row;
  align-items: center;
  width: 65vw;
  box-shadow: #bab0ac 1px 1px 8px;
  padding: 60px;
  align-content: center;
  height: 80vh;
}

.team-member {
  padding: 20px;
  border-radius: 10px;
  width: 100%;
  text-align: center;
  transition: transform 0.3s;
  display:flex;
  justify-content:center;
  height: 300px;
  align-items: center;
}
.team-info {
  width: 50%;
}
.team-info p{
  text-align: left;
    text-indent: 30px;
    line-height: 2;
    color: gray;
}
.alin{
  color: #f1f1f1;
  text-align: left;
  text-indent: 2em;
}

/* 方向 */
.direction {
text-align: center;
    background: white;
    padding: 50px 20px 100px 20px;
    display: flex
;
    flex-direction: column;
    align-content: center;
    align-items: center;
}

.direction-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 80px;
  margin-top: 20px;
  max-width: 1400px;
}

.direction-item {
  position: relative;
  overflow: hidden;
  border-radius: 8px;
  width: 250px;
}

.direction-item img {
  width: 100%;
  aspect-ratio: 5 / 9;
  object-fit: cover;
}

.direction-overlay {
  position: absolute;
  bottom: 20%;
  background: rgba(0, 0, 0, 0.7);
  color: white;
  padding: 15px;
  text-align: left;
  transition: background 0.3s;
}

.direction-item:hover .direction-overlay {
  background: rgba(51, 126, 255, 0.9);
}

.yg-title {
  font-size: 1.2rem;
  font-weight: bold;
}

.yg-desc {
  font-size: 0.9rem;
  opacity: 0.8;
}

/* 用户评价 */
.reviews {
  text-align: center;
  background-color: rgb(239, 240, 240);
  background: url(http://localhost:8080/img/comment_bk.db5add0b.png);
  padding: 30px 20px 100px 20px;
}

/* 教学功能 */
.features {
  text-align: center;
  background: white;
  padding: 50px 20px 150px 20px;
}

/* 统一交互动画 */
.stat:hover {
  transform: scale(1.1);
  transition: 0.3s ease-in-out;
}

.page-container {
  margin: 0 auto;
}

.scroll-animation {
  opacity: 0;
  transform: translateY(30px);
  transition: all 1s ease-out;
}

.scroll-visible {
  opacity: 1;
  transform: translateY(0);
}

</style>
